﻿using Interfaces.Models;
using Interfaces.Validation.Services;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace Framework.Validators.Session
{
    public class SessionExpiredValidator : Validator
    {
        private readonly ISessionValidationService _service;
        
        public SessionExpiredValidator(string messageTemplate, string tag, ISessionValidationService service) 
            : base(messageTemplate, tag)
        {
            _service = service;
        }

        public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            var session = objectToValidate as ISession;
            if(session!= null)
            {
              if(_service.IsSessionExpired(session.ChangeDate))
                LogValidationResult(validationResults, MessageTemplate, currentTarget, key);
            }
            else LogValidationResult(validationResults, "Session has incorrect format", currentTarget, key);
        }

        protected override string DefaultMessageTemplate
        {
            get { return "Session expired"; }
        }

       
    }
}
